import constants from './constants'
import OpenAI from 'openai'

const {GZJY, YEY, XX, CZ, ZKZZ, GZ, GK, GKZX, DX, OTHER, xueduanOrder, ZSZC, YWD, AREA} =
    constants

export async function askAi(content) {
    const openai = new OpenAI({
        apiKey: '6788fbf5-37c6-46b3-961d-eab5c6fcb99e',
        baseURL: 'https://ark.cn-beijing.volces.com/api/v3',
        dangerouslyAllowBrowser: true
    })
  const completion = await openai.chat.completions.create({
    messages: [{ role: 'user', content: content }],
    model: 'ep-20241118094221-f2nmw'
  })
  return completion.choices[0]
}

export function sleep(delay) {
    return new Promise((resolve) => setTimeout(resolve, delay))
}

export function getPercent(item, total) {
    return ((item.length / total.length) * 100).toFixed(2) + '%'
}

export function handleSheetData(sheetContent) {
    let xueduanLists = {
        [YEY]: [],
        [XX]: [],
        [CZ]: [],
        [ZKZZ]: [],
        [GZ]: [],
        [GK]: [],
        [DX]: [],
        [OTHER]: []
    }
    let areaLists = {}
    sheetContent.forEach((item) => {
        //处理学段
        if (item[ZSZC]) {
            let cleanedItemKey = item[ZSZC].replace(/[\n\s]/g, '') // 匹配换行符、空格和全角括号
                .replace(/（/g, '(')
                .replace(/）/g, ')')
            if (!xueduanLists[cleanedItemKey]) {
                if (xueduanOrder.includes(cleanedItemKey) || cleanedItemKey === GZJY) {
                    xueduanLists[cleanedItemKey] = []
                } else {
                    cleanedItemKey = OTHER
                }
            }
            xueduanLists[cleanedItemKey].push(item)
        } else {
            xueduanLists[OTHER].push(item)
        }
        //处理所属区域
        if (item[AREA]) {
            if (!areaLists[item[AREA]]) {
                areaLists[item[AREA]] = []
            }
            areaLists[item[AREA]].push(item)
        }
    })

    if (xueduanLists[GZJY]) {
        xueduanLists[GZ] = xueduanLists[GZ].concat(xueduanLists[GZJY])
        delete xueduanLists[GZJY]
    }

    if (xueduanLists[GKZX]) {
        xueduanLists[GK] = xueduanLists[GK].concat(xueduanLists[GKZX])
        delete xueduanLists[GKZX]
    }

    return {xueduanLists, areaLists}
}
